package com.cpi.cornpp.currency.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.currency.dto.CurrencyDTO;
import com.cpi.cornpp.currency.exception.CurrencyException;
import com.cs.common.pool.DBDAO;
import com.cs.common.pool.exception.PoolException;

public class CurrencyDAO extends DBDAO {
	private static final String SQL_CURRENY_LIST = "SELECT CURRENCY_ID, DESCRIPTION, APP_DEFAULT, SAP_CURRENCY FROM CURRENCIES";
	public List getCurrencies()throws CurrencyException{
		List currencyDTOs = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		CurrencyDTO currencyDTO = null;
		try{
			currencyDTOs = new ArrayList(0);
			preparedStatement = prepareSQL(SQL_CURRENY_LIST, dsName);
			resultSet = executeQuery(preparedStatement);
			while(resultSet.next()){
				currencyDTO = new CurrencyDTO();
				currencyDTO.setCurrencyId(resultSet.getString(1));
				currencyDTO.setDescription(resultSet.getString(2));
				currencyDTO.setSapCode(resultSet.getString(4));
				currencyDTOs.add(currencyDTO);
			}
		}catch(PoolException poolException){
			CurrencyException currencyException = null;
			poolException.printStackTrace(System.out);
			currencyException = new CurrencyException(poolException, CornPPException.LAYER_DAO, CornPPException.ACTION_LISTS);
			throw currencyException;
		}catch(SQLException sqlException){
			CurrencyException currencyException = null;
			sqlException.printStackTrace(System.out);
			currencyException = new CurrencyException(sqlException, CornPPException.LAYER_DAO, CornPPException.ACTION_LISTS);
			throw currencyException;
		}finally{
			try{
				closeConnection(preparedStatement.getConnection());
			}catch(SQLException sqlException){}
		}
		return currencyDTOs;
	}
}